<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload_target extends CI_Controller {

	function upload_target()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('encrypt');
		$this->load->model('mmaster');
		$this->load->model('mservices');
		$this->load->model('mterritory');
		$this->load->model('mupload_target');
		$this->load->model('musers');
		$this->is_logged_in();
	}

	function is_logged_in()
	{
		$is_logged_in = $this->session->userdata('is_logged_in');
		if(!isset($is_logged_in) || $is_logged_in != true)
		{
			redirect(base_url() .'index.php/login');
		}		
	}

	///////////////////////////////////////////
	///UPLOAD Target (MANUAL)//////////////////////
	///////////////////////////////////////////
	public function upload_manual_target() 
	{
		$data['template'] = "shell/smooth";
		$data['main_view'] = "f-upload-target";
		/*$todayDate = date("Y-m-d");// current date
		
		//get 3 next month
		for ($i=1; $i < 4 ; $i++) { 
			$date[$i] = strtotime(date("Y-m-d", strtotime($todayDate)) . "+".$i."month");
		}
		$data['choose_month'] = array(
                  'next_1_month'	=> date('m Y', $date[1]),
                  'next_2_month'  	=> date('m Y', $date[2]),
                  'next_3_month'  	=> date('m Y', $date[3])
      	);*/

		$data['error'] ="";
		$this->load->view($data['template'], $data);
		
	}
	
	function upload(){
        $data['template'] = "shell/smooth";
		$data['main_view'] = "f-upload-target";
		$data['error'] ="";
		if($_FILES['file_excel']['name'] <> "" ){
			$config['upload_path'] = './file/target/';
			$config['allowed_types'] = '*' ;
			$config['max_size'] = '2500000';
			$config['overwrite'] = TRUE;
			
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
			
			if(!$this->upload->do_upload("file_excel")){
				$data['error'] = $_FILES['file_excel']['type'].$this->upload->display_errors();
			}else{
				//baca file excel di sini
				$info = $this->upload->data();
				
				//if( $_FILES['file_excel']['type'] == 'application/vnd.ms-excel'){ //versi 2003
				if($info['file_ext'] == '.xls'){
					$this->load->library('PHPExcel');
					$objReader = new PHPExcel_Reader_Excel5();
					$objReader->setReadDataOnly(true);
					$objPHPExcel = $objReader->load( $info['full_path'] );
					$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
					$sheet = $objPHPExcel->getActiveSheet();
					$array_data = array();
					
					$i=1;
					//$this->db->trans_start(); 					// Save trans
					foreach($rowIterator as $row){
						if($i>1){
							$rowIndex = $row->getRowIndex ();							
							$d1 = $sheet->getCell('A' . $rowIndex); //cell region
							$d2 = $sheet->getCell('B' . $rowIndex); //cell cluster
							$d3 = $sheet->getCell('C' . $rowIndex); //cell nik
							$d4 = $sheet->getCell('D' . $rowIndex); //cell nama
							$d5 = $sheet->getCell('E' . $rowIndex); //cell kode_kpi
							$d6 = $sheet->getCell('F' . $rowIndex); //cell bulan
							$d7 = $sheet->getCell('G' . $rowIndex); //cell target

							$idata['nik'] = $d3->getCalculatedValue();  //konversi ke
							$idata['kpi_code'] = $d5->getCalculatedValue();
							$idata['kpi_target'] = $d7->getCalculatedValue();
							$idata['kpi_month'] = $d6->getCalculatedValue();
							$getTerritory = $this->mupload_target->getTerritory($d1->getCalculatedValue(),$d2->getCalculatedValue());
							$idata['istatus'] = 1;
							$idata['territory_id'] =  $getTerritory->{"f_get_cluster"};
							$this->mupload_target->addtargetmanual($idata);	// simpan ke database
						}
						$i++;
					}
				}
			}
			$this->load->view($data['template'], $data);
		}
    }
	
}